package com.tanhua.server.controller;

import com.alibaba.fastjson.JSONObject;
import com.tanhua.dubbo.server.pojo.Answer;
import com.tanhua.server.service.SoulService;
import com.tanhua.server.vo.ResultVo;
import com.tanhua.server.vo.SoulVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("testSoul")
public class SoulController {
    @Autowired
    private SoulService soulService;

    @GetMapping
    public ResponseEntity<List> testSoul(){
        try {
            List<SoulVo> list = soulService.getSoulTest();
            if (list != null) {
                return ResponseEntity.ok(list);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }
    @PostMapping
    public ResponseEntity<String> result(@RequestBody Map<Object,Answer[]> answers){
        try {
            Answer[] answers1 = answers.get("answers");
            String id = soulService.getResult(answers1);
            if (id != null) {
                return ResponseEntity.ok(id);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }
    @GetMapping("report/{id}")
    public ResponseEntity<ResultVo> getResult(@PathVariable("id") String id){
        try {
            ResultVo resultVo = soulService.getTestTable(id);
            if(resultVo != null){
                return ResponseEntity.ok(resultVo);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }
}
